//
//  GJSelectView.m
//  GJDigitalDemo
//
//  Created by wbb on 2021/11/8.
//

#import "GJSelectView.h"
#import <GJDigitalSDK/GJDigitalSDK.h>

@interface GJSelectView()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *hostTF;
@property (weak, nonatomic) IBOutlet UITextField *portTF;
@property (weak, nonatomic) IBOutlet UISwitch *cameraSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *asrSwitch;
@property (weak, nonatomic) IBOutlet UITextField *robotCodeTF;
@property (weak, nonatomic) IBOutlet UILabel *robotModeLab;
@property (weak, nonatomic) IBOutlet UIButton *menuBtn;
@property (weak, nonatomic) IBOutlet UILabel *dhrtcLab;
@property (weak, nonatomic) IBOutlet UIButton *groupBtn;
@property (weak, nonatomic) IBOutlet UILabel *asrlab;
@property (weak, nonatomic) IBOutlet UIButton *asrBtn;
@property (weak, nonatomic) IBOutlet UITextField *userIdTF;
@property (weak, nonatomic) IBOutlet UIView *asrBackView;

@end

@implementation GJSelectView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initEvent];
    }
    return self;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.hostTF.delegate = self;
    self.portTF.delegate = self;
    self.userIdTF.delegate = self;
    self.robotCodeTF.delegate = self;
    
    self.hostTF.text = @"new.guiji.ai";
    self.portTF.text = @"5222";
    self.userIdTF.text = @"iray-proxy@guiji.ai";
    self.robotCodeTF.text = @"230738573542952960";
    self.robotModeLab.text = @"客户端驱动";
    self.asrBackView.hidden = YES;
    
    [self initDataSource];
   
    [self initEvent];

}
- (void)initDataSource {
    [DigitalConfig shareConfig].serverIP = @"new.guiji.ai";
    [DigitalConfig shareConfig].servePort = 5222;
    [DigitalConfig shareConfig].robotCode = @"230738573542952960";
    [DigitalConfig shareConfig].robotMode = @"";
    [DigitalConfig shareConfig].stunList = @[@"stun:47.114.110.197:23478"];
    [DigitalConfig shareConfig].userJID = @"iray-proxy@guiji.ai";
    [DigitalConfig shareConfig].isShowLocalVideo = YES;
    [DigitalConfig shareConfig].isShowLocalAudio = YES;
    [DigitalConfig shareConfig].usingFrontCamera = YES;
    [DigitalConfig shareConfig].openAsr = @"true";
    [DigitalConfig shareConfig].groupId = @"dhrtcpc";
    [DigitalConfig shareConfig].isAsr = NO;
}
- (void)initEvent {
    UIAction *action1 = [UIAction actionWithTitle:@"客户端驱动" image:nil identifier:@"tianjia" handler:^(UIAction *action) {
        NSLog(@"客户端驱动");
        self.robotModeLab.text = @"客户端驱动";
        [DigitalConfig shareConfig].robotMode = @"";
    }];
    UIAction *action2 = [UIAction actionWithTitle:@"bot驱动" image:nil identifier:@"shanchu" handler:^(UIAction *action) {
        NSLog(@"bot驱动");
        self.robotModeLab.text = @"bot驱动";
        [DigitalConfig shareConfig].robotMode = @"bot";
    }];
    UIMenu * menu = [UIMenu menuWithTitle:@"" children:@[action1, action2]];
    self.menuBtn.menu = menu;
    
    
    UIAction *group_action1 = [UIAction actionWithTitle:@"dhrtcdev" image:nil identifier:@"tianjia" handler:^(UIAction *action) {
        self.dhrtcLab.text = @"dhrtcdev";
        [DigitalConfig shareConfig].groupId = @"dhrtcdev";
    }];
    UIAction *group_action2 = [UIAction actionWithTitle:@"dhrtcpc" image:nil identifier:@"shanchu" handler:^(UIAction *action) {
        self.dhrtcLab.text = @"dhrtcpc";
        [DigitalConfig shareConfig].groupId = @"dhrtcpc";
    }];
    UIMenu * group_menu = [UIMenu menuWithTitle:@"" children:@[group_action1, group_action2]];
    self.groupBtn.menu = group_menu;
    
    
    UIAction *asr_action1 = [UIAction actionWithTitle:@"一句话识别" image:nil identifier:@"tianjia" handler:^(UIAction *action) {
        self.asrlab.text = @"一句话识别";
        [DigitalConfig shareConfig].isAsr = NO;
        self.asrBackView.hidden = YES;
        
    }];
    UIAction *asr_action2 = [UIAction actionWithTitle:@"长链接识别" image:nil identifier:@"shanchu" handler:^(UIAction *action) {
        self.asrlab.text = @"长链接识别";
        [DigitalConfig shareConfig].isAsr = YES;
        self.asrBackView.hidden = NO;
    }];
    UIMenu * asr_menu = [UIMenu menuWithTitle:@"" children:@[asr_action1, asr_action2]];
    self.asrBtn.menu = asr_menu;
}

- (IBAction)startEvent:(UIButton *)sender {
    NSLog(@"startEvent");
    if (self.startBlock) {
        self.startBlock();
    }
}
- (IBAction)changeSwitch:(UISwitch *)sender {
    
    if (sender == self.cameraSwitch) {
        [DigitalConfig shareConfig].isShowLocalVideo = sender.on;
    }else if (sender == self.asrSwitch) {
        [DigitalConfig shareConfig].openAsr = sender.on ? @"true" : @"false";
    }
}


- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField == self.hostTF) {
        [DigitalConfig shareConfig].serverIP = textField.text;
    } else if (textField == self.portTF) {
        [DigitalConfig shareConfig].servePort = [textField.text integerValue];
    } else if (textField == self.robotCodeTF) {
        [DigitalConfig shareConfig].robotCode = textField.text;
    }else if (textField == self.userIdTF) {
        [DigitalConfig shareConfig].userJID = textField.text;
    }
}
@end
